﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace Chowpoo.Sql
{
    public class CommonBLL<T1, T2>
        where T1 : class, IDetach
        where T2 : CommonDal<T1>, new()
    {
        public static IQueryable<T1> GetList()
        {
            using (var dal = new T2())
            {
                var re = dal.SelectQueryable(o => true);
                return re;
            }
        }

        public static List<T1> GetAll()
        {
            using (var dal = new T2())
            {
                var re = dal.Select(o => true);
                return re;
            }
        }


        public static bool Del(IEnumerable<T1> list)
        {
            using (var dal = new T2())
            {
                return dal.Delete(list) != null;
            }
        }

        public static bool Del(T1 info)
        {
            using (var dal = new T2())
            {
                return dal.Delete(info) != null;
            }
        }

        public static bool DelAll()
        {
            using (var dal = new T2())
            {
                return dal.Delete(o=>true) != null;
            }
        }

        public static bool DelAll(Expression<Func<T1, bool>> expression)
        {
            using (var dal = new T2())
            {
                return dal.Delete(expression) != null;
            }
        }

        public static bool Edit(T1 info)
        {
            using (var dal = new T2())
            {
                return dal.Update(info) != null;
            }
        }

        public static bool Edit(IEnumerable<T1> info)
        {
            using (var dal = new T2())
            {
                return dal.Update(info) != null;
            }
        }

        public static bool Add(T1 info)
        {
            using (var dal = new T2())
            {
                return dal.Add(info) != null;
            }
        }

        public static bool Add(IEnumerable<T1> list)
        {
            using (var dal = new T2())
            {
                return dal.Add(list) != null;
            }
        }
    }
}
